<template>
  <div class="execution">
    <basic-container>
      <page-header-tool
        title="客户主数据"
        :btns="modBtns"
        :max="4"
      ></page-header-tool>
      <avue-crud
        class="avue-affixed avue-new-table"
        ref="crud"
        :page.sync="page"
        :data="tableData"
        :option="tableOption"
        :table-loading="listLoading"
        @refresh-change="searchChange"
        @current-change="currentChange"
        @size-change="sizeChange"
        @search-change="searchChange"
        @selection-change="selectionChange"
      >
        <template slot-scope="scope" slot="menu">
          <table-column-btns
            :btns="tableBtns"
            :scope="scope"
          ></table-column-btns>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { tableOption } from "./const/option.js";
import { tableBaseMixin } from "@/mixins/tableBase";
import { customerData } from "./const/data.js";
export default {
  name: "ledger",
  mixins: [tableBaseMixin],
  data() {
    return {
      modBtns: [
        {
          name: "新增",
          click: this.handleCreate,
          type: "success",
        },
      ],
      tableOption,
      selectionData: [],
      tableData: [],
      listLoading: false,
      tableBtns: [
        {
          name: "编辑",
          click: this.handleEditRow,
        },
        {
          name: "删除",
          click: this.handleDelete,
          color: "red",
        },
        {
          name: "启用",
          click: this.handleEnable,
          show: (row, index) => {
            return row.status === "0";
          },
        },
        {
          name: "停用",
          click: this.handleStop,
          color: "#E6A23C",
          show: (row, index) => {
            return row.status === "1";
          },
        },
      ],
    };
  },
  computed: {},
  created() {
    this.init();
  },
  methods: {
    init() {
      this.page.currentPage = 1;
      this.getList();
    },
    getList() {
      (this.tableData = customerData.records),
        (this.page.total = customerData.total);
    },

    /* C 新增人员主数据 */
    handleCreate() {
      this.$refs.modifyDialog.add();
    },

    /* U 编辑主数据 */
    handleEditRow(row, index) {
      this.$refs.modifyDialog.edit(row);
    },
    // 启用
    handleEnable(row) {
      this.$confirm("是否确认启用", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.EnableRequest(row.id);
      });
    },
    // 批量启用
    handleMultiEnable() {
      if (this.selectionData.length === 0) {
        return this.$message.error("请选择要操作内容");
      }
      this.$confirm("是否确认启用", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        let ids = [];
        try {
          ids = this.selectionData.map((i) => {
            if (i.status === "1") {
              throw { code: i.code, err: 1 };
            }
            return i.id;
          });
        } catch (e) {
          if (e.err === 1) {
            return this.$message.error(
              `货主编码：${e.code}, 非停用状态，无法启用！`
            );
          }
        }
        this.EnableRequest();
      });
    },
    EnableRequest() {},
    // 停用
    handleStop(row) {
      this.$confirm("是否确认停用", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.stopRequest(row.id);
      });
    },
    // 批量停用
    handleMultiStop() {
      if (this.selectionData.length === 0) {
        return this.$message.error("请选择要操作内容");
      }
      this.$confirm("是否确认停用", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        let ids = [];
        try {
          ids = this.selectionData.map((i) => {
            if (i.status === "0") {
              throw { code: i.code, err: 1 };
            }
            return i.id;
          });
        } catch (e) {
          if (e.err === 1) {
            return this.$message.error(
              `货主编码：${e.code}, 非启用状态，无法停用！`
            );
          }
        }
        this.stopRequest(ids.join(","));
      });
    },
    stopRequest(ids) {},
    // 批量删除
    handleMultiDelete() {
      if (this.selectionData.length === 0) {
        return this.$message.error("请选择要操作内容");
      }
      this.$confirm("是否确认删除该信息", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        let ids = [];
        ids = this.selectionData.map((i) => {
          return i.id;
        });
        this.deleteMultRequest(ids.join(","));
      });
    },
    deleteMultRequest(ids) {},
    /* 删除人数据 */
    handleDelete(row, index) {
      this.$confirm("是否确认删除该信息", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {});
    },
  },
};
</script>

<style lang="scss" scoped>
@import "~@/styles/variables.scss";

.app-container {
  padding: 10px 20px 20px 10px;
}
</style>
